class RouterPlugin {
  constructor ({ vue, router, Store }) {
    this.$router = router
    this.$vue = vue
    this.$store = Store
    this.$website = Store.getters['common/website']
    this.routerList = []
  }

  setTitle (title) {
    const defaultTitle = '管理后台'
    title = title ? `${title}-${defaultTitle}` : defaultTitle
    document.title = title
  }

  // 设置路由值
  getValue (route) {
    let value = ''
    if (route.query.src) {
      value = route.query.src
    } else {
      value = route.path
    }
    return value
  }

  // 处理菜单
  formatRoutes (menus, first) {
    console.log('111111')
    const aRouter = []
    const webSiteConfig = this.$website.menu.props
    const defaultProps = {
      label: webSiteConfig.label || 'label',
      path: webSiteConfig.path || 'url',
      icon: webSiteConfig.icon || 'icon',
      meta: webSiteConfig.meta || 'meta',
      children: webSiteConfig.children || 'children'
    }
    if (menus.length === 0) return
    for (let i = 0; i < menus.length; i++) {
      const menu = menus[i]
      const path = menu[defaultProps.path]
      const component = menu[defaultProps.path]
      const name = menu[defaultProps.label]
      const icon = menu[defaultProps.icon]
      const children = menu[defaultProps.children] || []
      const meta = menu[defaultProps.meta]
      const isChild = children.length !== 0
      const oRouter = {
        path: (() => {
          if (first) {
            return path
          } else {
            return path.slice(path.lastIndexOf('/') + 1)
          }
        })(),
        name: name,
        icon: icon,
        meta: meta,
        redirect: (() => {
          if (isChild && first) {
            const childrenUrl = menu.children[0][defaultProps.path]
            return `${childrenUrl}`
          } else {
            return ''
          }
        })(),
        component: (() => {
          // 判断是否为首路由
          if (isChild && first) {
            return () => import('../page/Index.vue')
          } else {
            return () => import(`@/views${component}`)
          }
        })(),
        children: !isChild ? (() => {
          if (first) {
            return [{
              component: (() => import(`@/views${component}.vue`))(),
              icon: icon,
              name: name,
              meta: meta,
              path: path
            }]
          }
          return []
        })() : (() => {
          return this.formatRoutes(children, false)
        })()
      }
      aRouter.push(oRouter)
    }
    if (first) {
      if (!this.routerList.includes(aRouter[0][defaultProps.path])) {
        aRouter.forEach(routeInfo => {
          if (Object.prototype.hasOwnProperty.call(routeInfo, 'children')) {
            // 暂放 满足条件 添加第一个参数 父路径的 name
            this.$router.addRoute(routeInfo.name, routeInfo)
          } else {
            this.$router.addRoute(routeInfo)
          }
          this.$router.options.routes.push(routeInfo)
        })
        this.routerList.push(aRouter[0][defaultProps.path])
      }
    } else {
      return aRouter
    }
  }
}
const userRouter = ({ Vue, Store, router }) => new RouterPlugin({ Vue, Store, router })
export default userRouter
